package org.smartjava.concurrent.core;

public class RunnableExample {
    public static void main(String[] args) {
        printNumInThreadRun(5);
    }

    public static void runnableForThread() {
        //通过匿名类的方式构造一个Runnable实例
        Runnable runnable = new Runnable() {
            //实现run方法
            @Override
            public void run() {
                //执行业务逻辑
                System.out.println("Runnable Example");
            }
        };
        //将Runnable实例作为参数传给Thread构造方法。
        Thread thread = new Thread(runnable);
        //启动线程
        thread.start();
    }


    public static void runnableForThread2() {
        new Thread(() -> System.out.println("Runnable Example")).start();
    }

    static void runnableForThreadWithLambda() {
        new Thread(() -> System.out.println("Runnable Example")).start();
    }

    public static void printNumInThread(int num) {
        for (int i = 0; i < num; i++) {
            int x = i;
            new Thread(() -> System.out.println(x)).start();
        }
    }

    public static void printNumInThreadRun(int num) {
        for (int i = 0; i < num; i++) {
            int x = i;
            new Thread(() -> System.out.println(x)).run();
        }
    }

    public static void printNumInRunnable(int num) {
        for (int i = 0; i < num; i++) {
            int x = i;
            Runnable runnable = () -> System.out.println(x);
            runnable.run();
        }
    }


}
